package com.woniuxy.vechicle.controller;

import com.woniuxy.vechicle.pojo.Vehicle;
import com.woniuxy.vechicle.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 车辆控制器
 */
@RestController
@RequestMapping("/api/vehicle")
@Slf4j
public class VehicleController {

    @Autowired
    private VehicleService vehicleService;

    /**
     * 根据车牌号查询车辆信息
     */
    @GetMapping("/check/{plateNumber}")
    public ResponseEntity<Vehicle> checkVehicle(@PathVariable String plateNumber) {
        log.info("接收到车辆查询请求 - 车牌号: {}", plateNumber);
        Vehicle vehicle = vehicleService.getByPlateNumber(plateNumber);
        if (vehicle != null) {
            log.info("查询到车辆信息 - 车牌号: {}, 状态: {}, 业主ID: {}", 
                plateNumber, vehicle.getStatus(), vehicle.getOwnerId());
            return ResponseEntity.ok(vehicle);
        } else {
            log.info("未查询到车辆信息 - 车牌号: {}", plateNumber);
            return ResponseEntity.ok().build(); // 返回200状态码但没有数据
        }
    }

    @GetMapping("/all")
    public ResponseEntity<List<Vehicle>> getAllVehicles() {
        List<Vehicle> vehicles = vehicleService.list();
        return ResponseEntity.ok(vehicles);
    }

    @PostMapping("/add")
    public ResponseEntity<Boolean> addVehicle(@RequestBody Vehicle vehicle) {
        boolean result = vehicleService.save(vehicle);
        return ResponseEntity.ok(result);
    }
} 